package org.nokatag.tagjava;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;


public class DateTime extends Inputsuper{
	//----- long as long 2009-1-15 下午02:22:48 描述 ----------------
	private static final long serialVersionUID = -783565660419885422L;
	private  JspWriter jspOut =null;
	private String prave="";
	private String isInitNowDate = "no";
	private String allownull="true";
	
	public int doStartTag()throws JspException{
		jspOut= pageContext.getOut();
		String pwidth = getWidth();
		Integer Iwidth =Integer.valueOf(LenNumber((pwidth==null)?"120":pwidth))-18;
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder stringBuilder = new StringBuilder();
		if(isInitNowDate.equalsIgnoreCase("yes")){
			String formatdate="yyyy-MM-dd";
			if(prave.indexOf("dateFmt")!=-1){
				for(Integer i=0;i<prave.split(",").length;i++){
					if(prave.split(",")[i].split(":'")[0].equalsIgnoreCase("dateFmt")){
						formatdate=prave.split(",")[i].split(":'")[1];
						formatdate=formatdate.substring(0,formatdate.length()-1);
					}
				}
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatdate); 
			String nowdate = bartDateFormat.format(new Date());
			setValue(nowdate);
		}
		
		if(prave.trim().length()>0 && !prave.startsWith(","))
			prave=","+prave;
		FormHtmlInsertid(getId(),getName(),getValue());
		stringBuilder.append(potjs(request));//加载Prototype
		stringBuilder.append(nkdateJS(request));//加载nkdateJS
		stringBuilder.append(selectCss(request));
		//--------------------------------------------------------------------------------------
		stringBuilder.append("<div style=\"float: left;\">");
		if(Equery("readonly", getReadonly()) || Equery("disabled", getDisabled()))
			stringBuilder.append("<div  class=\"date-dropdownchecklist\" id=\""+getId()+"_sdDiv\" style=\"width:"+Iwidth+"px;float:left;overflow:hidden;\">");
		else
			stringBuilder.append("<div  class=\"date-dropdownchecklist\"  id=\""+getId()+"_sdDiv\"  style=\"width:"+Iwidth+"px;float: left;overflow:hidden;\">");
		stringBuilder.append("<input type=\"text\" class=\"nk_input_lineheight nk_select_input_text\" style=\"width:"+Iwidth+"px;\" ");
		stringBuilder.append(getBaseStr());
		stringBuilder.append("/>");
		stringBuilder.append("</div>");
		stringBuilder.append("<div id=\""+getId()+"_dadiv\" style=\"float: left;\"></div>");
		stringBuilder.append("</div>");
		//------------------系统验证方法-----------------------------------------------------------
		stringBuilder.append("<script type=\"text/javascript\">");
		//---------------显示树形菜单---------------------------
		stringBuilder.append("Event.observe(document, 'dom:loaded', function() {");
		stringBuilder.append("var nda"+getId()+" = {");
		stringBuilder.append("id:'"+getId()+"',");//id
		stringBuilder.append("rurl:'"+getRootpath(request)+"',");//根url
		stringBuilder.append("prave:{el:'"+getId()+"'"+prave+"},");//根url
		stringBuilder.append("readonly:'"+(getReadonly()==null?"":getReadonly())+"',");//只读
		stringBuilder.append("disabled:'"+(getDisabled()==null?"":getDisabled())+"',");
		stringBuilder.append("allownull:"+allownull);
		stringBuilder.append("};");
		stringBuilder.append("var nd"+getId()+" = new ndateutil(nda"+getId()+");");
		stringBuilder.append("nd"+getId()+".show();");
		stringBuilder.append("});");
		stringBuilder.append("</script>");
		//--------------------------------------------------------------------------------------
		 try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public String getPrave() {
		return prave;
	}
	public void setPrave(String prave) {
		this.prave = prave;
	}
	public String getIsInitNowDate() {
		return isInitNowDate;
	}
	public void setIsInitNowDate(String isInitNowDate) {
		this.isInitNowDate = isInitNowDate;
	}
	
	public JspWriter getJspOut() {
		return jspOut;
	}
	public void setJspOut(JspWriter jspOut) {
		this.jspOut = jspOut;
	}
	public String getAllownull() {
		return allownull;
	}
	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}
}
